#![allow(non_snake_case)]
use icondata as i;
use leptos::prelude::*;
use leptos_icons::*;
use thaw_utils::Model;
#[component]
pub fn Pagination(
    #[prop(optional, into)] page: Model<usize>,
    #[prop(optional, into)] total: MaybeProp<usize>,
    #[prop(optional, into)] page_size: MaybeProp<usize>,
) -> impl IntoView {
    let page_count = Memo::new(move |_| {
        let total = total.get().unwrap_or(0);
        let page_size = page_size.get().unwrap_or(20);
        if total % page_size == 0 {
            total / page_size
        } else {
            total / page_size + 1
        }
    });
    let mid_pages = Memo::new(move |_| {
        let page_count = page_count.get();
        let mut curr_page = page.get();
        if curr_page >= page_count {
            curr_page = page_count
        }
        let mut index: usize = 1;
        if curr_page > 4 {
            index = curr_page - 2;
            if index + 4 > page_count {
                index = page_count - 4;
            }
        }
        let mut pages: Vec<usize> = Vec::new();
        while index <= page_count && pages.len() < 5 {
            pages.push(index);
            index += 1;
        }
        pages
    });
    let botton_view = move |p: usize| {
        view! {
            <button class="join-item btn"
             class=("bg-base-300",p==page.get())
             on:click=move |_| page.set(p)
            >{p}
            </button>
        }.into_any()
    };
    let page_views = move || {
        let page_count = page_count.get();
        if page_count ==0 {
            return None
        }
        let pages = mid_pages.get();
        let start = pages[0];
        let end = pages[pages.len() - 1];
        let mut page_views = Vec::new();
        if start != 1 {
            page_views.push(botton_view(1));
            page_views.push(view! {
                <button class="join-item btn" on:click=move |_| {if start>5 {page.set(start-5)} else {page.set(1)}}>...</button>
            }.into_any());
        }
        for p in pages.into_iter() {
            page_views.push(botton_view(p));
        }
        if end < page_count - 1 {
            page_views.push(view! {
                <button class="join-item btn" on:click=move |_| {if end+5<page_count {page.set(end+5)} else {page.set(page_count)}} >...</button>
            }.into_any());
        }
        if end != page_count {
            page_views.push(botton_view(page_count));
        }
        Some(page_views)
    };
    let total_str = move || format!("共{}行", total.get().unwrap_or(0));
    view! {
        <div class="join">
            <button class="join-item btn">
                {total_str}
            </button>
            <button class="join-item btn" class=("bg-base-300",move||page.get()==1) on:click=move |_| {if page.get_untracked()>1 {page.update(|v|*v=*v-1)}}>
                <Icon icon=i::BsChevronLeft/>
            </button>
            {page_views}
            <button class="join-item btn" class=("bg-base-300",move||page.get()==page_count.get()) on:click=move |_| {if page.get_untracked()<page_count.get_untracked() {page.update(|v|*v=*v+1)}}>
                <Icon icon=i::BsChevronRight/>
            </button>
        </div>
    }
}
